package com.cbgolf.oa.activity.publiz;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cbgolf.oa.R;
import com.cbgolf.oa.base.BaseActivity;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.rsa.RSAUtils;
import com.cbgolf.oa.util.AnimaUtil;
import com.cbgolf.oa.util.DataUtil;
import com.cbgolf.oa.util.Util;
import com.cbgolf.oa.widget.autolayout.utils.AutoUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by seiya on 2017/1/4.
 */

public class PwdModifyActivity extends BaseActivity {

    @ViewInject(R.id.topBack_ll)
    private View topBack;
    @ViewInject(R.id.title_tv)
    private TextView topTitle;

    @ViewInject(R.id.a_pwd_modify_old_et)
    private EditText oldEt;
    @ViewInject(R.id.a_pwd_modify_new_et)
    private EditText newEt;
    @ViewInject(R.id.a_pwd_modify_affirm_et)
    private EditText affirmEt;

    @ViewInject(R.id.a_pwd_modify_submit_tv)
    private TextView subMitTv;

    private String oldPwd;
    private String newPwd;
    private String affirmPwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a_pwd_modify);
        AutoUtil.auto(this);
    }

    @Override
    protected void findViews() {
        x.view().inject(this);
    }

    @Override
    protected void setViews() {
        topTitle.setText("修改密码");
    }

    @Override
    protected void setListener() {
        topBack.setOnClickListener(v -> finish());
        subMitTv.setOnClickListener(v -> checkInfo());
    }

    @Override
    protected Activity getContext() {
        return this;
    }

    private void checkInfo() {
        oldPwd = oldEt.getText().toString();
        newPwd = newEt.getText().toString();
        affirmPwd = affirmEt.getText().toString();
        if (Util.isNull(oldPwd.trim())) {
            Util.show("请输入原密码");
            return;
        }
        if (oldPwd.length() < 6 || oldPwd.length() > 12) {
            Util.show("原密码不正确");
            return;
        }
        if (Util.isNull(newPwd.trim())) {
            Util.show("请输入新密码");
            return;
        }
        if (newPwd.length() < 6 || newPwd.length() > 12) {
            Util.show("密码必须为6-12位数字或字母");
            return;
        }
        if (Util.isNull(affirmPwd.trim())) {
            Util.show("请输入确认密码");
            return;
        }
        if (!affirmPwd.equals(newPwd)) {
            Util.show("前后密码不一致");
            return;
        }
        getRsa();
    }

    private void getRsa() {
        Util.checkNet();
        String url = WebAPI.pwd_rsa_get;
        Web.getOK(url, new NetCallBack(response -> {
            if (200 != response.statusCode || Util.isNull(response.result)) {
                Util.showNetError();
                return;
            }
            String exponent;
            String modulus;
            try {
                com.alibaba.fastjson.JSONObject json = JSON.parseObject(response.result);
                exponent = json.getString("empoent");
                modulus = json.getString("module");
                RSAPublicKey pubkey = RSAUtils.getRSAPublidKey(modulus, exponent);
                String pwdNew = RSAUtils.encryptString(pubkey, newPwd);
                String pwdOld = RSAUtils.encryptString(pubkey, oldPwd);
                modifyPwd(pwdOld, pwdNew);
            } catch (Exception e) {
                Util.showNetError();
            }
        }));


    }


    private void modifyPwd(String pwdOld, String pwdNew) {
        String url = WebAPI.pwd_modify_put + "/" + DataUtil.getUserId();
        JSONObject json = new JSONObject();
        final Map<String, Object> map = new HashMap<>();
        try {
            json.put("newPassword", pwdNew);
            json.put("oldPassword", pwdOld);
            json.put("loginType", "ANDROID");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("dto", json);
        map.put("token", DataUtil.getToken());
        AnimaUtil.showLoading(context);
        Web.putjsonOk(url, map, new NetCallBack(response -> {
            AnimaUtil.stopLoading();
            if (200 != response.statusCode) {
                if (!Util.isNull(response.errorMsg)) {
                    Util.show(response.errorMsg);
                    return;
                }
                Util.showNetError();
                return;
            }
            Util.show("修改成功");
            finish();
        }));

    }

    @Override
    protected void onKeyBack() {
        finish();
    }
}
